from redisearch import Client, IndexDefinition, TextField, Query
from redisearch import reducers
from redisearch.aggregation import AggregateRequest, Asc
from redis import ResponseError
import datetime

def insert_document(client):
    # 约定字段，此处为tile、name2个字段
    client.create_index((TextField('title'), TextField('name')))
    # 插入document,相当于关系型数据库的一行数据
    client.add_document('doc1', title='你好Redisearch1', name='人工智能Python1', language='chinese')
    client.add_document('doc2', title='你好Redisearch2', name='人工智能Python2', language='chinese')
    client.add_document('doc3', title='你好Redisearch3', name='人工智能Python3', language='chinese')




if __name__ == '__main__':

    START_TIME = datetime.now().strftime("%Y-%m-%d-%H:%M.%S")

    SCHEMA = (
        TextField("title", weight=5.0),
        TextField("body")
    )

    # 连接索引
    # client = Client("my-index")
    # client = Client("my-index", password="my-password")
    # definition = IndexDefinition(prefix=['blog:'])

    # try:
    #     print("打印索引信息：")
    #     client.info()
    #     client.create_index(SCHEMA, definition=definition)
    # except ResponseError:
    #     print("ERROR")
    #     # 创建索引定义和模式
    #     # client.create_index(SCHEMA, definition=definition)
    # 索引文件
    # client.add_document('doc2', title='你好', body='我在北京学习人工智能', language='chinese')

    # 创建索引，相当于关系型数据库的表
    client = Client("index1")

    # insert_document(client)
    # client.add_document('doc4', title='人工智能', name='插入第4个document数据, language='chinese') # 可以检索到
    # client.add_document('doc5', title='人工智能测试数据', name='插入第5个document数据')  # 不能检索到
    # client.add_document('doc6', title='人工智能测试数据', name='插入第6个document数据6', language='chinese') #可以检索到
    # client.add_document('doc7', title='人工智能测试数据7', name='插入第7个document数据7', language='chinese')
    # client.add_document('doc8', title='人工智能测试数据8', name='abcdefghijk')  # 不能检索到
    client.add_document()

    # 查找搜索
    # res = client.search("人工智能")  # 可以检索到
    # res = client.search("人工") # 不能检索索到
    # res = client.search("测试") # 可以检索索到
    # res = client.search("智能")  # 不能检索索到
    # res = client.search("你好")  # 可以检索到
    # res = client.search("search")  # 不能检索到
    # res = client.search("Python")  # 不能检索到
    # data = client.get("doc7")
    # print(data)
    # 拼音检索
    res = client.search("abcdefghijk")  # 可以检索索到

    # 查询所有
    # res = client.search("*")
    # # 条件查询
    # query_filter = "人工智能"  # 可以检索到
    # query_filter = "@title:你好"  # 可以检索到
    # query_filter = "@title:Redisearch" # 不能检索索到
    # query_filter = "@name:人工智能"  # 可以检索索到
    # query_filter = "@name:人工"  # 不能检索索到
    # query_filter = "@name:智能"  # 不能检索索到
    # query = Query(query_filter).language("chinese") # language 不生效
    # query = Query(query_filter)
    # res = client.search(query)
    # 删除document
    # client.delete_document("doc4")

    # print(res.docs[0].title)
    # print(res.docs[1].title)
    # print(res.docs[2].title)
    # Result{3 total, docs: [Document {'id': 'doc3', 'payload': None, 'title': '你好Redisearch3', 'name': '人工智能Python2'}, Document {'id': 'doc2', 'payload': None, 'title': '你好Redisearch2', 'name': '人工智能Python2'}, Document {'id': 'doc1', 'payload': None, 'title': '你好Redisearch1', 'name': '人工智能Python1'}]}
    print(res.docs)
    # print(client.info())

    ##
    # request = AggregateRequest('*').group_by(
    #     ['@published_year'], reducers.avg('average_rating').alias('average_rating_for_year')
    # ).sort_by(
    #     Asc('@average_rating_for_year')
    # ).limit(
    #     0, 10
    # ).filter('@published_year > 0')
    # ##

